var p=1;
function tiaozhuan(p){
	var adComName=$(".adComName").val();
	var pageSize=$('.ui-select-pagesize').val();
	var d={};
	d.pageSize=pageSize;
	d.adComName=adComName;
	d.page=p;
	d.curUserId=token_id;
	$.ajax({
		url: url + "/sys/fiWithdrawCom/queryPage",
		type: 'POST',
		//		参数需转为字符串
		data: JSON.stringify(d),
		//		json跨域
		dataType: 'json',
		contentType: "application/json",
		beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				var data=result.data.rows;
				var html="";
				for(var i in data){
					html += '<tr class="ng-scope"><td class="ng-binding">'+data[i].adComId+'</td>';
					html += '<td class="ng-binding">'+data[i].adComName+'</td><td class="ng-binding">'+data[i].balance+'</td>';
					html += '<td class="ng-binding">'+data[i].totalProfit+'</td><td class="ng-binding">'+data[i].undrawnProfit+'</td>';
					html += '<td class="ng-binding">'+data[i].drawnProfit+'</td><td>';
					html += '<a href="trade_information.html'+token_str+'&acid='+data[i].adComId+'">查看记录</a>&nbsp;&nbsp;&nbsp;&nbsp;';
					html += '<a onclick="showProfits('+data[i].adComId+','+data[i].undrawnProfit+')" class="ng-scope">提取竞价交易利润</a></td></tr>';
				}
				$(".list_profit").html(html);
				var r=result.data.total;
					paging(p,pageSize,r);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	}); 
}
tiaozhuan(1);

//下载文件
function download(){
	var param2 = "/sys/fiWithdrawCom/queryPage";
	var method2 = "post";
	var adComName=$(".adComName").val();
	var datas2 = {page: p, pageSize: 1000, adComName: adComName};
	getAgentMsg2(param2,method2,datas2);
	exportCsv({
        title: ['广告主ID','广告公司名称','余额','总利润','未提取利润','已提取利润'],
        titleForKey: ['adComId','adComName','balance','totalProfit','undrawnProfit','drawnProfit'],
        data: dataResult2.data.rows,
    },"利润提取.csv");
}

//搜索按钮
$(".adComName_btn").on("click",function(){
	p=1;
	tiaozhuan(p);
})

//利润提取弹框
function showProfits(id,kk) {
	$(".amount_btn").attr("data",id);
	$(".kke").html(kk);
	$(".profits_amount").val("");
    layer.open({
        type: 1,
        title: "利润提取",
        content: $('.profits'),
        area:["422px","250px"],
		end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
			$(".profits").css("display","none");
		}
    })
}
//利润提取按钮
var ppll_profits=true;
$(".amount_btn").on("click",function(){
	if(ppll_profits){
		ppll_profits=false;
		var amount=$(".profits_amount").val();
		if(!amount){
			layer.msg("请输入提取利润金额");
			ppll_profits = true;
			return false;
		}
		var d={};
		d.amount=amount;
		d.adComId=$(this).attr("data");
		$.ajax({
			url: url + "/sys/fiWithdrawCom/withdrawCompetitionByAdComId",
			type: 'POST',
			//		参数需转为字符串
			data: JSON.stringify(d),
			//		json跨域
			dataType: 'json',
			contentType: "application/json",
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("X-Token", token); 
			},
			useDefaultXhrHeader: false,
			success: function(result) {
				if(result.code == '0') {
					layer.msg(result.msg,{time:2*1000},function() {
						p=1;
						tiaozhuan(p);
						layer.closeAll();
					});
				} else {
					isErr(result);
				}
			},
			err: function(err) {
				layer.msg("请求失败");
			},
			complete:function(){
				ppll_profits = true;
			}
		});
	}
})